// ============================================================================
// 日志系统
// ============================================================================
import { logger, LogHandler, LogEntry, Logger, LogLevel } from '@roasmax/logger'


// ============================================================================
// 控制台传输器
// ============================================================================
/**
 * 控制台日志处理器
 */
class ConsoleHandler implements LogHandler {
  constructor(private enableColors: boolean = true) {}

  handle(entry: LogEntry): void {
    const timestamp = new Date().toISOString()
    const levelName = LogLevel[entry.level]
    const prefix = `[${timestamp}] [${levelName}] [${entry.loggerName}]`

    const message = this.enableColors 
      ? this.colorizeMessage(prefix, entry.message, entry.level)
      : `${prefix} ${entry.message}`

    switch (entry.level) {
      case LogLevel.ERROR:
        console.error(message, ...entry.args)
        break
      case LogLevel.WARN:
        console.warn(message, ...entry.args)
        break
      case LogLevel.INFO:
        console.info(message, ...entry.args)
        break
      case LogLevel.DEBUG:
        console.debug(message, ...entry.args)
        break
      default:
        console.log(message, ...entry.args)
    }
  }

  private colorizeMessage(prefix: string, message: string, level: LogLevel): string {
    const colors = {
      [LogLevel.ERROR]: '\x1b[31m',   // 红色
      [LogLevel.WARN]: '\x1b[33m',    // 黄色
      [LogLevel.INFO]: '\x1b[36m',    // 青色
      [LogLevel.DEBUG]: '\x1b[37m'    // 白色
    }
    
    const reset = '\x1b[0m'
    const color = colors[level] || ''
    
    return `${color}${prefix}${reset} ${message}`
  }
}

Logger.addHandler(new ConsoleHandler())
export { logger }
